{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "b32f3139-4c24-4b59-8c55-84ef060bfea4",
   "metadata": {},
   "source": [
    "# 一、Jupyter Notebook"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7e261d34-d813-4fb6-ab72-9ee5ba3dc880",
   "metadata": {},
   "source": [
    "## 1.常用快捷键"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5f496873-91fe-458c-9eda-c148311f83de",
   "metadata": {},
   "source": [
    "编辑模式（Enter开启）：\n",
    "- Tap:代码补全或缩进\n",
    "- Shift + Tab ：提示\n",
    "- Shift + Enter :运行本单元，选中下单元(如果没有下单元，则创建选中下单元)\n",
    "- Ctrl + Enter ：运行本单元\n",
    "- Alt + Enter ：运行本单元，在下面插入一单元"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3e2840c0-32d3-4aab-8cf5-d62979bfbfb9",
   "metadata": {},
   "source": [
    "命令模式（ESC开启）：\n",
    "- Shift + Enter ：运行本单元，选中下单元(如果没有下单元，则创建选中下单元)\n",
    "- Ctrl + Enter ：运行本单元\n",
    "- Alt + Enter ：运行本单元，在下面插入一单元\n",
    "- Y ：单元转为代码状态\n",
    "- M ：单元转为markdown状态\n",
    "- A :在上方插入新单元\n",
    "- B ：在下方插入新单元\n",
    "- DD ：删除选中单元"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "44ffb844-f2ac-4be0-aa1a-340c25694cf3",
   "metadata": {},
   "source": [
    "## 2. IPython帮助文档"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6e3f563b-7c2c-4f20-859f-9527d979398c",
   "metadata": {},
   "source": [
    "### 1. 使用help()\n",
    "通过使用`help()`获得帮助文档"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "076e5069-4dab-4192-96e1-b6385c4ca382",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function len in module builtins:\n",
      "\n",
      "len(obj, /)\n",
      "    Return the number of items in a container.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(len)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "36e0650c-7a0f-4d31-99a8-ec3db033c9e4",
   "metadata": {},
   "source": [
    "### 2. 使用?\n",
    "通过使用`?`获得帮助文档"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "id": "2997f01c-e369-4c4c-8a50-f27f509307ca",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "\u001b[1;31mSignature:\u001b[0m \u001b[0mlen\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mobj\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;33m/\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
       "\u001b[1;31mDocstring:\u001b[0m Return the number of items in a container.\n",
       "\u001b[1;31mType:\u001b[0m      builtin_function_or_method"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "len?"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "20ed674c-c6f4-45c2-b1f3-0d2fa239c459",
   "metadata": {},
   "source": [
    "还可以通过双问好把函数的源代码显示出来"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "id": "720d2c8e-58c2-4e64-adbd-3c625a24d1a3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "\u001b[1;31mSignature:\u001b[0m \u001b[0madd\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0ma\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mb\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
       "\u001b[1;31mDocstring:\u001b[0m <no docstring>\n",
       "\u001b[1;31mSource:\u001b[0m   \n",
       "\u001b[1;32mdef\u001b[0m \u001b[0madd\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0ma\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mb\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\n",
       "\u001b[0m    \u001b[1;32mreturn\u001b[0m \u001b[0ma\u001b[0m\u001b[1;33m+\u001b[0m\u001b[0mb\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
       "\u001b[1;31mFile:\u001b[0m      c:\\users\\21232\\appdata\\local\\temp\\ipykernel_10028\\516529982.py\n",
       "\u001b[1;31mType:\u001b[0m      function"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "def add(a,b):\n",
    "    return a+b\n",
    "\n",
    "add??"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1dd81480-899a-4dc5-949a-528d5a947bfc",
   "metadata": {},
   "source": [
    "## 3. IPython魔法命令\n",
    "\n",
    "### 1.运行外部Python文件\n",
    "通过使用`%run *.py`来执行外部python文件"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "id": "118c72ae-8e8b-432f-a3de-39464335d981",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Hello Word\n",
      "HelloIPython\n"
     ]
    }
   ],
   "source": [
    "%run Hello.py\n",
    "say('IPython')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "43a343e8-1432-4011-b722-c5a6fa377d56",
   "metadata": {},
   "source": [
    "## 2. 运行计时\n",
    "\n",
    "使用`%time 代码`命令可以计算代码运行时间"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 70,
   "id": "175af0b9-a568-47d0-a599-1a07eaf90411",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "HelloIpython\n",
      "CPU times: total: 0 ns\n",
      "Wall time: 0 ns\n"
     ]
    }
   ],
   "source": [
    "%time say('Ipython')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ef938111-8eb7-427b-b42d-1aca406c152c",
   "metadata": {},
   "source": [
    "使用time计算运行时间较短的代码无法得到精确的运行时间\n",
    "这时就可以使用`%timeit`来计算代码运行时长"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 85,
   "id": "5dbfe0e4-9d87-4c9d-90de-fc2df32f31dd",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "42.2 μs ± 2.14 μs per loop (mean ± std. dev. of 7 runs, 10,000 loops each)\n"
     ]
    }
   ],
   "source": [
    "def fun(n):\n",
    "    a=0\n",
    "    for i in range(n):a+=i\n",
    "    return a\n",
    "%timeit fun(1000)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5b3ae313-d4ad-4897-b11c-39771cafecd9",
   "metadata": {},
   "source": [
    "可以使用两个百分号来测试多行代码的平均运行时间"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 110,
   "id": "33a17087-c202-4bb1-b57b-daa55efaa973",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "933 ns ± 31.4 ns per loop (mean ± std. dev. of 7 runs, 1,000,000 loops each)\n"
     ]
    }
   ],
   "source": [
    "%%timeit \n",
    "\n",
    "def fun(n):\n",
    "    a=0\n",
    "    for i in range(n):a+=i\n",
    "    return a\n",
    "\n",
    "fun(10)\n",
    "fun(20)\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e32ab7d4-c2d0-4464-a7d8-d15bc971fb77",
   "metadata": {},
   "outputs": [],
   "source": [
    "%%time\n",
    "def fun(n):\n",
    "    a=0\n",
    "    for i in range(n):a+=i\n",
    "    return a\n",
    "\n",
    "fun(1000)\n",
    "fun(2000)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "81448e09-7cb5-41c2-9992-5e75504bc39a",
   "metadata": {},
   "source": [
    "## 3. 查看当前会话中的所有变量与函数\n",
    "通过`%who`可以查看当前会话的所有变量与函数名称"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5194f69a-92fe-4755-b751-aecdf0685507",
   "metadata": {},
   "outputs": [],
   "source": [
    "%who\n",
    "a=10\n",
    "name='zhangsan'\n",
    "def say():print('Hello Word')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c8f030da-6d15-483f-b59e-4d1c8bd0b20a",
   "metadata": {},
   "source": [
    "`%whos`查看详情\n",
    "`%who_ls` 以列表的形式返回\n",
    "\n",
    "\n",
    "### 4. 查看其他魔法命令\n",
    "`lsmagic`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "309517ca-5442-4acc-9707-cb63559baf2d",
   "metadata": {},
   "outputs": [],
   "source": [
    "lsmagic"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
